package com.snowfield.lbcommon.service.impl;

import com.snowfield.lbcommon.domain.Cluster;
import com.snowfield.lbcommon.mapper.ClusterMapper;
import com.snowfield.lbcommon.service.IClusterService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 负载集群Service业务层处理
 *
 * @author snowfield
 * @date 2024-03-20
 */
@Service
public class ClusterServiceImpl implements IClusterService {
    @Autowired
    private ClusterMapper clusterMapper;

    /**
     * 查询负载集群
     *
     * @param id 负载集群主键
     * @return 负载集群
     */
    @Override
    public Cluster selectClusterById(Long id) {
        Cluster cluster = clusterMapper.selectClusterById(id);
        if (cluster.getVirrouterId() > 0) {
            String status = "UNKNOWN";
            String statusFilePath = "/etc/keepalived/keepalived_statusVI_" + cluster.getVirrouterId() + ".log";
            try {
                if (new File(statusFilePath).exists()) {
                    List<String> contentList = FileUtils.readLines(new File(statusFilePath), StandardCharsets.UTF_8);
                    if (contentList != null && contentList.size() > 0) {
                        status = contentList.get(0);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            cluster.setStatus(status);
        }
        if (cluster.getVirrouterId2() > 0) {
            String status = "UNKNOWN";
            String statusFilePath = "/etc/keepalived/keepalived_statusVI_" + cluster.getVirrouterId2() + ".log";
            try {
                if (new File(statusFilePath).exists()) {
                    List<String> contentList = FileUtils.readLines(new File(statusFilePath), StandardCharsets.UTF_8);
                    if (contentList != null && contentList.size() > 0) {
                        status = contentList.get(0);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            cluster.setStatus2(status);
        }
        return cluster;
    }

    /**
     * 查询负载集群列表
     *
     * @param cluster 负载集群
     * @return 负载集群
     */
    @Override
    public List<Cluster> selectClusterList(Cluster cluster) {
        return clusterMapper.selectClusterList(cluster);
    }

    /**
     * 修改负载集群
     *
     * @param cluster 负载集群
     * @return 结果
     */
    @Override
    public int updateCluster(Cluster cluster) {
        //删除旧的VI实例的状态文件
        Cluster oldCluster = clusterMapper.selectClusterById(cluster.getId());
        String statusFilePath = "/etc/keepalived/keepalived_statusVI_" + oldCluster.getVirrouterId() + ".log";
        String statusFilePath2 = "/etc/keepalived/keepalived_statusVI_" + oldCluster.getVirrouterId2() + ".log";
        if (new File(statusFilePath).exists()) {
            FileUtils.deleteQuietly(new File(statusFilePath));
        }
        if (new File(statusFilePath2).exists()) {
            FileUtils.deleteQuietly(new File(statusFilePath2));
        }

        return clusterMapper.updateCluster(cluster);
    }

}
